$(function() {
    var reportEditor;
    KindEditor.ready(function(K) {
        var options = {
            width: '100%',
            height: '600px',
            resizeType: 0,
            themeType: 'selfcss',
            items: [
                'undo', 'redo', '|', 'fontname', 'fontsize', 'bold', 'italic', 'underline', '|', 'insertorderdlist', 'insertunorderedlist', 'indent', 'outdent', '|', 'image', 'emoticons', 'table', 'media', '|', 'source', '/', 'strikethrough', 'forecolor', 'hilitecolor', 'link', 'unlink', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'formatblock', '|', 'hr', 'flash', 'code', '|', 'preview'
            ],
            uploadJson: '../editor/jsp/upload_json.jsp',
            fileManagerJson: '../editor/jsp/file_manager_json.jsp',
            allowFileManager: true
        };
        reportEditor = K.create('#report', options);
    });

    var cfileReader = new fileReader();
    cfileReader.init("#cover", function(src) {
        if ($(".imgPreview").find("img").length === 0) {
            $(".imgPreview").append("<img src=\'" + src + "\'>");

        } else {
            $(".imgPreview").find("img").attr("src", src);
        }
    });

    $("#submit-btn").on("click", function() {
        var url = "api_editReport";
        var datas = {};
        if (!$("body").attr("data-id")) {
            datas = {
                report_name: $("#name").val(),
                cover: $("#match_cover img").attr("src"),
                content: reportEditor.html()
            };
        } else {
            datas = {
            	content_id:$("body").attr("data-content-id"),
                report_id: $("body").attr("data-id"),
                report_name: $("#name").val(),
                cover: $("#match_cover img").attr("src"),
                content: reportEditor.html()
            }
        }
        $.post(url, datas, function(data) {
            if (data.state == 200) {
                window.location.href = "/xqyq/manage/report.html"
            } else {
                alert(data.comment);
            }
        }, "json");
    });
})
